package com.example.cd5160866.urlapp.models2;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by cd5160866 on 16/5/10.
 */
public class ModelRecyclerAdapterV2<T> extends RecyclerView.Adapter<ModelRecyclerAdapterV2.ModelViewHolder> {
    public static final int TYPE_DEFULT = 1000;
    public static final int TYPE_HEADER = 1001;
    public static final int TYPE_FOOTER = 1002;
    protected Context mContext;
    protected int mLayoutId;
    private Class<? extends ModelViewHolder> viewHolderClass;
    protected List<T> mDatas = new ArrayList<>();

    public ModelRecyclerAdapterV2(Context context, Class<ModelViewHolder> viewHolderClass) {
        this.mContext = context;
        this.viewHolderClass = viewHolderClass;
        this.mLayoutId = viewHolderClass.getAnnotation(ItemViewId.class)
                .value();
    }

    public ModelRecyclerAdapterV2(Context context, Class<ModelViewHolder> viewHolderClass, List<T> Datas) {
        this.mContext = context;
        this.viewHolderClass = viewHolderClass;
        this.mLayoutId = viewHolderClass.getAnnotation(ItemViewId.class)
                .value();
        this.mDatas = Datas;
    }

    @Override
    public ModelViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        ModelViewHolder viewHolder = null;
        try {
            View converView = null;
            switch (viewType) {
                case TYPE_DEFULT:
                    converView = LayoutInflater.from(mContext).inflate(mLayoutId, parent, false);
                    break;
                case TYPE_HEADER:
                    converView = headerView;
                    break;
                case TYPE_FOOTER:
                    converView = footerView;
                    break;
            }
            viewHolder = viewHolderClass.getConstructor(View.class).newInstance(converView);
            viewHolder.viewType = viewType;
            ButterKnife.inject(viewHolder, converView);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ModelViewHolder holder, int position) {
        Log.d("qw", holder.viewType + "");
        if (holder.viewType == TYPE_DEFULT) {
            if (headerView != null)
                position = position - 1;
            holder.update(mDatas.get(position), this, mContext, position);
        }
    }

    @Override
    public int getItemViewType(int position) {
        int itemViewType = TYPE_DEFULT;
        if (itemSize == 0)
            return itemViewType;
        if (position == 0) {
            if (headerView != null)
                itemViewType = TYPE_HEADER;
        } else if (position == itemSize - 1) {
            if (footerView != null)
                itemViewType = TYPE_FOOTER;
        }
        return itemViewType;
    }

    private View headerView;

    private View footerView;

    private int itemSize = 0;

    @Override
    public int getItemCount() {
        itemSize = mDatas.size();
        if (headerView != null)
            itemSize++;
        if (footerView != null)
            itemSize++;
        return itemSize;
    }

    public void add(int position, T data, Handler handler) {
        mDatas.add(position, data);
        if (headerView != null) {
            position++;
        }
        notifyItemInserted(position);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                notifyDataSetChanged();
            }
        }, 500);

    }

    public void remove(int position, Handler handler) {
        mDatas.remove(position);
        if (headerView != null) {
            position++;
        }
        notifyItemRemoved(position);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                notifyDataSetChanged();
            }
        }, 500);
    }

    public void add(List<T> data) {
        mDatas.clear();
        mDatas.addAll(data);
        notifyDataSetChanged();

    }

    public void addAll(List<T> datas) {
        mDatas.addAll(datas);
        notifyDataSetChanged();
    }

    public void clear() {
        mDatas.clear();
        notifyDataSetChanged();
    }

    public View getHeaderView() {
        return headerView;
    }

    public void setHeaderView(View headerView) {
        this.headerView = headerView;
        this.notifyDataSetChanged();
    }

    public View getFooterView() {
        return footerView;
    }

    public void setFooterView(View footerView) {
        this.footerView = footerView;
        this.notifyDataSetChanged();
    }

    public static abstract class ModelViewHolder<T> extends RecyclerView.ViewHolder {

        public int viewType;

        public ModelViewHolder(View itemView) {
            super(itemView);
        }

        public abstract void update(T item, ModelRecyclerAdapterV2 adapter, Context context, int positon);
    }
}
